package com.Xht.www.util;

import java.util.Map;

public class ValidationUtil {

    //验证登录或注册输入的合法性工具
    public static ValidationResult validateInput(String type, Map<String, String> params) {
        boolean isValid = true;
        StringBuilder message = new StringBuilder();

        switch (type) {
            case "user-login":
                String username = params.get("username");
                String password = params.get("password");
                if (username == null || !username.matches("[a-zA-Z0-9]+")) {
                    isValid = false;
                    message.append("用户名只能包含字母和数字 ");
                }
                if (password == null || password.length() < 6) {
                    isValid = false;
                    message.append("密码长度至少为6位 ");
                }
                break;

            case "merchant-login":
                String merchantName = params.get("merchantName");
                String merchantPassword = params.get("password");
                if (merchantName == null || !merchantName.matches("[a-zA-Z0-9]+")) {
                    isValid = false;
                    message.append("商家名称只能包含字母和数字 ");
                }
                if (merchantPassword == null || merchantPassword.length() < 6) {
                    isValid = false;
                    message.append("密码长度至少为6位 ");
                }
                break;

            case "admin-login":
                String adminName = params.get("adminName");
                String adminPassword = params.get("password");
                if (adminName == null || !adminName.matches("[a-zA-Z0-9]+")) {
                    isValid = false;
                    message.append("管理员名称只能包含字母和数字 ");
                }
                if (adminPassword == null || adminPassword.length() < 6) {
                    isValid = false;
                    message.append("密码长度至少为6位 ");
                }
                break;

            case "user-register":
                String newUser = params.get("username");
                String newPassword = params.get("password");
                String confirmPassword = params.get("confirmPassword");
                String gender = params.get("gender");
                String birthday = params.get("birthday");

                if (newUser == null || !newUser.matches("[a-zA-Z0-9]+")) {
                    isValid = false;
                    message.append("用户名只能包含字母和数字 ");
                }
                if (newPassword == null || newPassword.length() < 6) {
                    isValid = false;
                    message.append("密码长度至少为6位 ");
                }
                if (newPassword != null && !newPassword.equals(confirmPassword)) {
                    isValid = false;
                    message.append("两次输入的密码不一致 ");
                }
                if (gender == null || (!gender.equals("男") && !gender.equals("女"))) {
                    isValid = false;
                    message.append("性别必须为“男”或“女” ");
                }
                if (birthday == null || birthday.isEmpty()) {
                    isValid = false;
                    message.append("生日不能为空 ");
                }
                break;

            case "merchant-register":
                String newMerchant = params.get("merchantName");
                String newMerchantPassword = params.get("password");
                String confirmMerchantPassword = params.get("confirmPassword");
                String address = params.get("address");
                String establishmentYear = params.get("establishmentYear");
                String contactInfo = params.get("contactInfo");
                String keywords = params.get("keywords");

                if (newMerchant == null || !newMerchant.matches("[a-zA-Z0-9]+")) {
                    isValid = false;
                    message.append("商家名称只能包含字母和数字 ");
                }
                if (newMerchantPassword == null || newMerchantPassword.length() < 6) {
                    isValid = false;
                    message.append("密码长度至少为6位 ");
                }
                if (newMerchantPassword != null && !newMerchantPassword.equals(confirmMerchantPassword)) {
                    isValid = false;
                    message.append("两次输入的密码不一致 ");
                }
                if (address == null || address.isEmpty()) {
                    isValid = false;
                    message.append("地址不能为空 ");
                }
                if (establishmentYear == null || !establishmentYear.matches("\\d{4}")) {
                    isValid = false;
                    message.append("成立年份应为四位数字 ");
                }
                if (contactInfo == null || contactInfo.isEmpty()) {
                    isValid = false;
                    message.append("联系信息不能为空 ");
                }
                if (keywords == null || keywords.isEmpty()) {
                    isValid = false;
                    message.append("关键词不能为空 ");
                }
                break;

            case "admin-register":
                String newAdmin = params.get("adminName");
                String newAdminPassword = params.get("password");
                String confirmAdminPassword = params.get("confirmPassword");

                if (newAdmin == null || !newAdmin.matches("[a-zA-Z0-9]+")) {
                    isValid = false;
                    message.append("管理员名称只能包含字母和数字 ");
                }
                if (newAdminPassword == null || newAdminPassword.length() < 6) {
                    isValid = false;
                    message.append("密码长度至少为6位 ");
                }
                if (newAdminPassword != null && !newAdminPassword.equals(confirmAdminPassword)) {
                    isValid = false;
                    message.append("两次输入的密码不一致 ");
                }
                break;

            default:
                isValid = false;
                message.append("无效的操作类型");
                break;
        }

        return new ValidationResult(isValid, message.toString().trim());
    }

    //验证结果封装类

    public static class ValidationResult {
        private final boolean success;
        private final String message;

        public ValidationResult(boolean success, String message) {
            this.success = success;
            this.message = message;
        }

        public boolean isSuccess() {
            return success;
        }

        public String getMessage() {
            return message;
        }
    }
}
